package com.open.loan.task;

import com.alibaba.fastjson.JSON;		
import com.alibaba.fastjson.JSONObject;
import com.open.common.task.AlipayTest1;
import com.open.loan.utils.sms.SmsTry;
import com.open.system.domain.LoanApplyDO;
import com.open.system.domain.LoanApplyLogDO;
import com.open.system.domain.LoanMsgConfigDO;
import com.open.system.service.LoanApplyLogService;
import com.open.system.service.LoanApplyService;
import com.open.system.service.LoanMsgConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;

@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private LoanMsgConfigService loanMsgConfigService;

    @Autowired
    private LoanApplyLogService loanApplyLogService;

    @Autowired
    private LoanApplyService loanApplyService;

    public static ArrayBlockingQueue creditCardArrayBlockingQueue = new ArrayBlockingQueue(1000);
    public static ArrayBlockingQueue alipayCardArrayBlockingQueue = new ArrayBlockingQueue(1000);

    /**
     * 信用卡查询账单
     *
     * @throws Exception
     */
  /*  @Scheduled(fixedRate = 5000)
    public void doJuxinliCredit() throws Exception {
        for (int i = 0; i < creditCardArrayBlockingQueue.size(); i++) {
            String token = (String) creditCardArrayBlockingQueue.take();
            JSONObject jsonObjectCode = CreditCardTest.startCaijiByCode(token);
            System.out.println(jsonObjectCode.toJSONString());
        }
    }*/

    /**
     * 支付宝查询账单
     *
     * @throws Exception
     */
   /* @Scheduled(fixedRate = 5000)
    public void doJuxinliAlipay() throws Exception {
        for (int i = 0; i < alipayCardArrayBlockingQueue.size(); i++) {
            String token = (String) alipayCardArrayBlockingQueue.take();
            AlipayTest1 alipayTest1 = new AlipayTest1();
            JSONObject jsonObjectCode = alipayTest1.GetCodeReturnInfo(token);
            System.out.println(jsonObjectCode.toJSONString());
        }
    }*/


    /**
     * 按用户for发送
     *
     * @throws Exception
     */
    //@Scheduled(fixedRate = 5000)
    public void sendMsg() throws Exception {
        LoanMsgConfigDO loanMsgConfigDO = loanMsgConfigService.get(Long.parseLong("1"));
        Date now = new Date();
        if (loanMsgConfigDO != null) {
            //一天发一次
            if (StringUtils.isNotEmpty(loanMsgConfigDO.getRemark1()) &&
                    (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(loanMsgConfigDO.getRemark1()).getTime() - now.getTime()) / (24 * 60 * 60 * 1000) < 1) {
                return;
            }
            HashMap hashMap = new HashMap<String, Object>();
            //hashMap.put("userId","249");
            List<LoanApplyLogDO> loanApplyLogDO = loanApplyLogService.list(hashMap);
            for (int i = 0; i < loanApplyLogDO.size(); i++) {
                if (loanApplyLogDO.get(i).getRepaymentTime() != null &&
                        (loanApplyLogDO.get(i).getRepaymentTime().getTime() - now.getTime()) / (24 * 60 * 60 * 1000)
                                < Integer.parseInt(loanMsgConfigDO.getPreDay())) {
                    //发短信。。。
                    System.out.println(loanMsgConfigDO.getContent());

                    hashMap = new HashMap<String, Object>();
                    hashMap.put("userId", loanApplyLogDO.get(i).getUserId());
                    List<LoanApplyDO> loanApplyDOList = loanApplyService.list(hashMap);

                    String phone = "";
                    for (int j = 0; j < loanApplyDOList.size(); j++) {
                        try {
                            phone = loanApplyDOList.get(j).getRemark6();
                            //SMS_143660251短信模板要换
                            //new SmsTools().sendMessage(phone, JSON.toJSONString(params), "SMS_143660251");
                            new SmsTry().sendsms(phone,"45986",loanMsgConfigDO.getContent());
                            //更新发送时间
                            loanMsgConfigDO.setRemark1(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                            loanMsgConfigService.update(loanMsgConfigDO);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }

    }

    /**
     * 按手机号发短信
     *
     * @throws Exception
     */
    //@Scheduled(fixedRate = 5000)
    public void sendMsgByTel() throws Exception {
        Map<String, Object> hashMap = new HashMap<>();

        List<LoanMsgConfigDO> LoanMsgConfigDOList = loanMsgConfigService.list(hashMap);
        Date now = new Date();
        LoanMsgConfigDO loanMsgConfigDO = null;
        for (int i = 0; i < LoanMsgConfigDOList.size(); i++) {
            loanMsgConfigDO = LoanMsgConfigDOList.get(i);

            //一天发一次
            if (StringUtils.isNotEmpty(loanMsgConfigDO.getRemark1()) &&
                    (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(loanMsgConfigDO.getRemark1()).getTime() - now.getTime()) / (24 * 60 * 60 * 1000) < 1) {
                continue;
            }

            //依手机号码查询是否有借款用户
            hashMap.put("remark6", loanMsgConfigDO.getRemark2());
            List<LoanApplyDO> LoanApplyDOList = loanApplyService.list(hashMap);

            if (LoanApplyDOList.size() > 0) {
                hashMap.clear();
                hashMap.put("userId", loanMsgConfigDO.getRemark2());
            } else {
                continue;
            }

            //只发查询到的第一条记录
            hashMap.put("userId", LoanApplyDOList.get(0).getUserId());
            List<LoanApplyLogDO> loanApplyLogDO = loanApplyLogService.list(hashMap);
            String phone = LoanApplyDOList.get(0).getRemark6();
            for (int ii = 0; ii < loanApplyLogDO.size(); ii++) {
                //比较接口时间跟设置的提前几天是否满足条件
                if (loanApplyLogDO.get(ii).getRepaymentTime() != null &&
                        (loanApplyLogDO.get(ii).getRepaymentTime().getTime() - now.getTime()) / (24 * 60 * 60 * 1000)
                                < Integer.parseInt(loanMsgConfigDO.getPreDay())) {
                    try {
                        Map<String, Object> params = new HashMap<>();
                        params.put("code", loanMsgConfigDO.getContent());
                        //SMS_143660251短信模板要换
                        //new SmsTools().sendMessage(phone, JSON.toJSONString(params), "SMS_143660251");
                        new SmsTry().sendsms(phone,"45986",loanMsgConfigDO.getContent());
                        //更新发送时间
                        loanMsgConfigDO.setRemark1(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                        loanMsgConfigService.update(loanMsgConfigDO);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}